﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
using System.IO;
using Facebook.Json;
using Facebook.Configuration;

namespace Facebook.Api.Generator.Tests
{
    /// <summary>
    /// Summary description for FeedsControllerTetss
    /// </summary>
    [TestClass]
    public class FeedsControllerTests
    {
        public FeedsControllerTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void PublishUserAction()
        {
            var fb = Helpers.GetDesktopContext();

            Helpers.EstablishSession(ref fb);

            var story = new UserActionStory(76809339608)
            {
                TargetIds = new List<Int64> { 1386564816 },
                StorySize = FeedStorySize.TwoLine
            };
            
            Assert.IsTrue(fb.Feed.PublishUserAction(story));
        }

        public void InitTemplatesConfig()
        {
            using (var file = File.Open(FacebookUtility.MapPath(Facebook.Configuration.FacebookSection.Current.FeedTemplateConfigSource), FileMode.Create))
            {
                using (var assemblyStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Facebook.Api.Generator.Tests." + Facebook.Configuration.FacebookSection.Current.FeedTemplateConfigSource))
                {
                    using (var writer = new StreamWriter(file))
                    {
                        using (var reader = new StreamReader(assemblyStream))
                        {
                            writer.Write(reader.ReadToEnd());
                        }
                    }
                }
            }
        }

        [TestMethod]
        public void InitTemplateBundles()
        {
            this.InitTemplatesConfig();

            var fb = Helpers.GetDesktopContext();

            fb.Feed.InitFeedTemplates();
        }

        [TestMethod]
        public void RegisterTemplateBundle()
        {
            var fb = Helpers.GetDesktopContext();
            
            String oneLineTemplate = "{*actor*}'s registerTemplateBundle unit test!";
            var shortStory = new StoryTemplateElement { Title = "{*actor*}'s registerTemplateBundle unit test!", Body = "foo" };
            var actionLink = new FeedActionLinkElement { Href = "http://dandoes.net/", Text = "DanDoes.NET" };

            var publishResult = fb.Feed.RegisterTemplateBundle(
                new String[] { JsonConvert.SerializeObject(new String[] { oneLineTemplate }) },
                new String[] { JsonConvert.SerializeObject(new StoryTemplateElement[] { shortStory }) },
                new String[] { JsonConvert.SerializeObject(new FeedActionLinkElement[] { actionLink}) });

            Assert.IsFalse(publishResult.IsError, "RegisterTemplateBundle returned an error: {0}", publishResult.IsError ? publishResult.ResponseException.Message : String.Empty);
            Assert.IsTrue(publishResult.Value > 0);

            var getResult = fb.Feed.GetRegisteredTemplateBundleByID(publishResult.Value.ToString());

            Assert.IsFalse(getResult.IsError, "GetRegisteredTemplateBundleByID returned an error: {0}", getResult.IsError ? getResult.ResponseException.Message : String.Empty);
            Assert.IsNotNull(getResult.Value);
            Assert.AreEqual(oneLineTemplate, getResult.Value.OneLineStoryTemplates.FirstOrDefault());
            Assert.AreEqual(shortStory.Title, getResult.Value.ShortStoryTemplates.First().TemplateTitle);
            Assert.AreEqual(shortStory.Body, getResult.Value.ShortStoryTemplates.First().TemplateBody);
        }
    }
}
